/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.model.css;

public class Background extends Style {
    protected String attachment;
    protected String color;
    protected String image;
    protected String position;
    protected String repeat;

    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public String getAttachment() {
        return attachment;
    }

    public String getImage() {
        return image;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getPosition() {
        return position;
    }

    public void setRepeat(String repeat) {
        this.repeat = repeat;
    }

    public String getRepeat() {
        return repeat;
    }

    public String getStyle() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(addProperty("background-color", color, null));
        buffer.append(addProperty("background-image", image, null));
        buffer.append(addProperty("background-position", position, null));
        buffer.append(addProperty("background-repeat", repeat, null));
        buffer.append(addProperty("background-attachment", attachment, null));

        return buffer.toString();
    }

    public void setStyleValue(String pValue) {
        if (pValue.indexOf("background-color") != -1) {
            color = pValue.substring(pValue.indexOf(":") + 1);
        }

        if (pValue.indexOf("background-image") != -1) {
            image = pValue.substring(pValue.indexOf(":") + 1);
        }

        if (pValue.indexOf("background-position") != -1) {
            position = pValue.substring(pValue.indexOf(":") + 1);
        }

        if (pValue.indexOf("background-repeat") != -1) {
            repeat = pValue.substring(pValue.indexOf(":") + 1);
        }

        if (pValue.indexOf("background-attachment") != -1) {
            attachment = pValue.substring(pValue.indexOf(":") + 1);
        }
    }

    public static void main(String[] args) {
        String colorStr = "background-repeat:repeat;margin-top:1pt;margin-left:1pt;margin-right:1pt;margin-bottom:1pt;background-attachment:scroll;";
        Background borderStyle = new Background();
        borderStyle.setStyle(colorStr);
        System.out.println(borderStyle.getStyle());
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setImage(String image) {
        if ((image == null) || (image.trim().length() == 0)) {
            this.image = null;

            return;
        }

        if (image.indexOf("url") != -1) {
            this.image = image;
        } else {
            this.image = "url(" + image + ")";
        }
    }

    public String toString() {
        return getStyle();
    }
}
